﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using CommunitySystem.Common;
using CommunitySystem.Ctrls;

namespace CommunitySystem
{
    public partial class InterviewTemplete2Window : ChildWindow
    {
        private MouseTool _mouseTool = new MouseTool();
        private ObservableCollection<CompetorClass> chooseparams = null;
        private WordActionService.WordActionClient _client = null;
        private WordActionService.InterviewTemplete _entity = null;
        public InterviewTemplete2Window(int id)
        {
            InitializeComponent();
            chooseparams = new ObservableCollection<CompetorClass>();
            chooseparams.Add(new CompetorClass() { Name = "姓名", Sign = "{姓名}" });
            chooseparams.Add(new CompetorClass() { Name = "面试时间", Sign = "{面试时间}" });
            chooseparams.Add(new CompetorClass() { Name = "面试地点", Sign = "{面试地点}" });
            cbAlign.ItemsSource = new string[3] { "左对齐", "居中", "右对齐" };
            //this.LayoutRoot.AddHandler(Grid.MouseLeftButtonDownEvent, new MouseButtonEventHandler(MouseLeftButtonDown), true);
            //rtb.AddHandler(RichTextBox.MouseLeftButtonUpEvent, new MouseButtonEventHandler(rtb_MouseLeftButtonUp), true);
            //this.LayoutRoot.MouseLeftButtonUp += new MouseButtonEventHandler(rtb_MouseLeftButtonUp);
            //this.LayoutRoot.MouseLeftButtonDown += new MouseButtonEventHandler(MouseLeftButtonDown);
            //chooseparams.Add(new CompetorClass() { Name = "面试时间", Sign = "{面试时间}" });
            //chooseparams.Add(new CompetorClass() { Name = "面试时间", Sign = "{面试时间}" });
            lbListBox.ItemsSource = chooseparams;
            _client = new WordActionService.WordActionClient();
            _client.SaveInterviewTempleteCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功！");
                    this.DialogResult = true;
                }
            };
            if (id > 0)
            {
                _client.GetInterViewTempleteCompleted += (sender, e) =>
                {
                    _entity = e.Result.FirstOrDefault();
                    DataBinding();
                };
                _client.GetInterViewTempleteAsync("id=" + id);
            }
            else
            {
                _entity = new WordActionService.InterviewTemplete()
                {
                    ID =0,
                    Name = tbName.Text,
                    TemContent = rtb.Xaml,
                    //CreateDate = DateTime.Now,
                    //ModifyDate = DateTime.Now,
                    CreateUser = EHR.Common.Session.LoginUser.LoginName,
                    ModifyUser = EHR.Common.Session.LoginUser.LoginName
                };
            }
           
            //lbListBox.DisplayMemberPath = "Name";
            //rtb.AllowDrop = true;        
        }

        private void DataBinding()
        {
            if (_entity != null)
            {
                tbName.Text = _entity.Name;
                rtb.Xaml = _entity.TemContent;
                //Emily
              // TempType.SelectedValue=_entity.TZMBLX;
              
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbName.Text))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("模版名称不能为空");
                return;
            }
            if (string.IsNullOrEmpty(rtb.Xaml))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("模版内容不能为空");
                return;
            }
            //Emily
            //if (this.TempType.SelectedItem == null)
            //{
            //    SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择模板类别！");
            //    return;
            //}
          //  _entity.TZMBLX = (this.TempType.SelectedItem as CommunitySystem.DataService.DataDictionaryItemModel).Id;
            _entity.Name = tbName.Text;
            _entity.TemContent = rtb.Xaml;
            if (_entity.ID <= 0)
            {
                _entity.CreateDate = DateTime.Now;
            }
            _entity.ModifyDate = DateTime.Now;
            _entity.ModifyUser = EHR.Common.Session.LoginUser.LoginName;
            _entity.IsActive = true;
            _client.SaveInterviewTempleteAsync(_entity);
            //this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        //加粗
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (rtb != null)
            {
                if (rtb.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight && ((FontWeight)rtb.Selection.GetPropertyValue(Run.FontWeightProperty)) == FontWeights.Normal)
                    rtb.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
                else
                    rtb.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal);
                
            }
        }

        private void BtnUnderLine_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (rtb != null)
            {
                if (rtb.Selection.GetPropertyValue(Run.TextDecorationsProperty) == null)
                    rtb.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
                else
                    rtb.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, null);
            }
        }

        #region 窗口移动

        static bool isSecect = false;//是否选中
        static Point startPoint = new Point();//开始坐标
        static FrameworkElement currentElement = null;//要拖动的控件
        //鼠标抬起
        public static void MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isSecect)
            {
                currentElement.ReleaseMouseCapture();//移除鼠标捕获
            }
            isSecect = false;
        }

        //鼠标按下
        public  void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (lbListBox.SelectedItem != null)
            {
                //var cc = lbListBox.SelectedItem as CompetorClass;
                //TextBlock tbc = new TextBlock() { Text = cc.Name, DataContext = cc, Width = 120, Height = 40, Visibility = System.Windows.Visibility.Visible, Foreground = new SolidColorBrush(Colors.Red) };
                currentElement = (sender as FrameworkElement);//获取要拖动的控件
                currentElement.CaptureMouse();//鼠标捕获
                isSecect = true;//选中
                startPoint = e.GetPosition(null);//获取当前坐标
            }
        }

        //鼠标移动
        public  void MouseMove(object sender, MouseEventArgs e)
        {
            if (isSecect)
            {
                Point pt = e.GetPosition(null);//获取当前坐标
                //判断距离下边框和右边框是否超过范围，为了防止拖出到容器外
                //if (pt.Y - this.ActualHeight < -30 & pt.X - this.ActualWidth < -30)
                //{
                    Matrix mx = (currentElement.RenderTransform as MatrixTransform).Matrix;
                    mx.OffsetX += pt.X - startPoint.X;//用当前坐标减去起始坐标获取移动距离
                    mx.OffsetY += pt.Y - startPoint.Y;//用当前坐标减去起始坐标获取移动距离
                    currentElement.RenderTransform = new MatrixTransform() { Matrix = mx };//重新移位
                    startPoint = pt;//把当前坐标获取为起始坐标
               // }
            }
        }
        #endregion


        private void InsertParam(string param)
        {

        }

        private void rtb_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isSecect)
            {
                currentElement.ReleaseMouseCapture();//移除鼠标捕获
            }
            isSecect = false;
        }

        private void tb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this._mouseTool.IsDoubleClick())
            {
                var cc = lbListBox.SelectedItem as CompetorClass;
                if (cc != null)
                {
                    //InlineUIContainer ui = new InlineUIContainer() { }//{ Child = new TextBlock() { Text = cc.Sign, DataContext = cc } };
                    rtb.Selection.Text = cc.Sign;
                }
            }
        }

        private void cbAlign_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbAlign.SelectedValue == null)
                return;
            TextAlignment align = TextAlignment.Left;
            switch (this.cbAlign.SelectedItem.ToString())
            {
                case "左对齐":
                    align = TextAlignment.Left;
                    break;
                case "居中":
                    align = TextAlignment.Center;
                    break;
                case "右对齐":
                    align = TextAlignment.Right;
                    break;
            }
            this.rtb.Selection.ApplyPropertyValue(Paragraph.TextAlignmentProperty, align);
        }

        

        private void rtb_SelectionChanged(object sender, RoutedEventArgs e)
        {
            try
            {
                switch ((TextAlignment)this.rtb.Selection.GetPropertyValue(Block.TextAlignmentProperty))
                {
                    case TextAlignment.Left:
                        this.cbAlign.SelectedItem = "左对齐";
                        break;
                    case TextAlignment.Center:
                        this.cbAlign.SelectedItem = "居中";
                        break;
                    case TextAlignment.Right:
                        this.cbAlign.SelectedItem = "右对齐";
                        break;
                }
            }
            catch { }
        }

       
    }

}

